<template>
  <div>
    <el-form :model="condition" size="small" :inline="true">
      <el-form-item label="账号">
        <el-input v-model="condition.username" placeholder="请输入账号" clearable />
      </el-form-item>
      <el-form-item label="是否异常">
        <el-select v-model="condition.error" placeholder="请选择是否异常" clearable>
          <el-option v-for="(s,index) in errorArr" :key="index" :label="s" :value="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作日期">
        <el-date-picker
          v-model="condition.operationTimes"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        <el-button icon="el-icon-refresh-left" @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="height: 34px">
      <el-pagination background align="right" v-show="total > 0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="pageSizes"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next"
                     :total="total">
      </el-pagination>
    </div>
    <!-- el-table的data属性就是用来绑定这个表格显示的数据 -->
    <el-table :data="logs" height="250" style="width: 100%">
      <!-- el-table-column表示表格的每一列信息 label属性表示表头的名称
         prop表示绑定的字段名 width属性表示这一列显示的宽度-->
      <el-table-column prop="id" label="日志编号" width="60px"/>
      <el-table-column prop="url" label="请求地址" width="100px"/>
      <el-table-column prop="parameter" label="请求参数" show-overflow-tooltip/>
      <el-table-column prop="operationTime" label="操作时间"/>
      <el-table-column prop="user" label="操作账号"/>
      <el-table-column prop="error" label="是否异常">
        <template slot-scope="scope">
          <span v-if="scope.row.error === 0">正常</span>
          <span v-else>异常</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "Log",
  data(){
    return {
      condition: {
        username: '',
        error: '',
        operationTimes: []
      },
      errorArr: ['正常', '异常'],
      logs: [],
      currentPage: 1, //分页的默认页码应该是第一页
      pageSizes: [50, 100, 150, 200, 400], //可选的每页显示条数
      pageSize: 50, //每页显示条数
      total: 0 //查询的总条数，默认值为0，查询之后才会赋值
    }
  },
  methods: {
    handleSizeChange(val){//这个函数是用来监听每页显示条数的变更
      this.currentPage = 1
      this.pageSize = val
      this.search()
    },
    handleCurrentChange(val){//这个函数是用来监听当前页码变化
      this.currentPage = val
      this.search()
    },
    search(){
      this.doGet('/log', {
        username: this.condition.username,
        error: this.condition.error,
        dateFrom: this.condition.operationTimes.length === 2 ? this.condition.operationTimes[0] : '',
        dateTo: this.condition.operationTimes.length === 2 ? this.condition.operationTimes[1] : '',
        currentPage: this.currentPage,
        pageSize: this.pageSize
      }, resp => {
        this.logs = resp.records
        this.total = resp.total
      })
    },
    reset(){
      this. condition = {
        username: '',
        error: '',
        operationTimes: []
      }
      this.search()
    }
  },
  mounted() { //这个函数是一个钩子函数，有vue自动调用，在页面完成挂载之后就会调用这个函数
    this.search()
  }
}
</script>

<style scoped>

</style>
